//
//  SWOrderStatusView.swift
//  ChildStory
//
//  Created by linhan on 2017/1/20.
//  Copyright © 2017年 Aiya. All rights reserved.
//

import Foundation
import UIKit
class SWOrderStatusView: UIView
{
    var status:SWOrderStatus = .unknow{
        didSet{
            if status != oldValue
            {
                updateProgressBar(status)
                updateFlagIcon(status)
            }
        }
    }
    
    
    private var _progressBarMaxWidth:CGFloat = 0
    private var _icons:[UIImageView] = []
    
    
    private var _progressBar:UIView = UIView()
    
    override init(frame:CGRect)
    {
        super.init(frame: frame)
        setup()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setup()
    {
        let titleLabel:UILabel = UILabel()
        titleLabel.text = "订单状态"
        titleLabel.textColor = UIColor.darkGray
        titleLabel.font = UIFont.systemFont(ofSize: 12)
        titleLabel.sizeToFit()
        addSubview(titleLabel)
        
        let progressView = UIView()
        progressView.frame = CGRectMake(0, titleLabel.bottom, width, 30)
        progressView.backgroundColor = UIColor(white: 0.94, alpha: 1)
        addSubview(progressView)
        
        let purchasedIcon:UIImageView = createFlagIcon(tag: SWOrderStatus.purchased.index)
        let uploadedIcon:UIImageView = createFlagIcon(tag: SWOrderStatus.uploaded.index)
        let makingIcon:UIImageView = createFlagIcon(tag: SWOrderStatus.making.index)
        let shippedIcon:UIImageView = createFlagIcon(tag: SWOrderStatus.shipped.index)
        let deliveredIcon:UIImageView = createFlagIcon(tag: SWOrderStatus.delivered.index)
        
        progressView.addSubview(purchasedIcon)
        progressView.addSubview(uploadedIcon)
        progressView.addSubview(makingIcon)
        progressView.addSubview(shippedIcon)
        progressView.addSubview(deliveredIcon)
        
        _icons.append(purchasedIcon)
        _icons.append(uploadedIcon)
        _icons.append(makingIcon)
        _icons.append(shippedIcon)
        _icons.append(deliveredIcon)
        
        LayoutUtil.distributeHAverageWelt(_icons, containerWidth: progressView.width)
        
        createFlagLabel(status: .purchased, center: purchasedIcon.center, container: progressView)
        createFlagLabel(status: .uploaded, center: uploadedIcon.center, container: progressView)
        createFlagLabel(status: .making, center: makingIcon.center, container: progressView)
        createFlagLabel(status: .shipped, center: shippedIcon.center, container: progressView)
        createFlagLabel(status: .delivered, center: deliveredIcon.center, container: progressView)
        
        _progressBarMaxWidth = progressView.width - purchasedIcon.width
        _progressBar.frame = CGRectMake(purchasedIcon.width * 0.5, 0, 0, 8)
        _progressBar.backgroundColor = UIColor.lightGray
        progressView.insertSubview(_progressBar, at: 0)
        
    }
    private var _normalImage:UIImage? = UIImage(named: "child_radio_normal")
    private var _selectedImage:UIImage? = UIImage(named: "child_radio_selected")
    private func createFlagIcon(tag:Int) -> UIImageView
    {
        let imageView = UIImageView(image: _normalImage, highlightedImage: _selectedImage)
        imageView.sizeToFit()
        imageView.tag = tag
        return imageView
    }
    
    private func createFlagLabel(status:SWOrderStatus, center:CGPoint, container:UIView)
    {
        let label:UILabel = UILabel()
        label.font = UIFont.systemFont(ofSize: 12)
        label.text = status.name
        label.sizeToFit()
        label.center = CGPointMake(center.x, center.y + 10)
        container.addSubview(label)
    }
    
    //根据状态决定进度
    private func updateProgressBar(_ status:SWOrderStatus)
    {
        var percent:CGFloat = 0
        switch status {
        case .unknow,.unpaid,.purchased:
            percent = 0
        case .uploaded:
            percent = 0.25
        case .making:
            percent = 0.5
        case .shipped:
            percent = 0.75
        case .delivered:
            percent = 1
        }
        _progressBar.width = percent * _progressBarMaxWidth
    }
    
    private func updateFlagIcon(_ status:SWOrderStatus)
    {
        for icon in _icons
        {
            //icon.isHighlighted = false
            icon.image = _normalImage
            if status.index >= icon.tag
            {
                icon.image = _selectedImage
                //icon.isHighlighted = true
            }
        }
    }
    
    
    
    
}
